package cn.kawa.controller;

import cn.kawa.feign.MovieFeignClient;
import cn.kawa.service.IUserService;
import cn.kawa.domain.User;
import cn.kawa.query.UserQuery;
import cn.kawa.result.JsonResult;
import cn.kawa.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;
    @Autowired
    public MovieFeignClient movieFeignClient;

    /**
     * 功能1.，展示票的列表
     */
    @RequestMapping(value="/ticket/{id}",method=RequestMethod.GET)
    public JsonResult ticket(@PathVariable("id") Long id){
        return movieFeignClient.get(id);
    }
    @RequestMapping(value="/ticket/list",method=RequestMethod.GET)
    public JsonResult ticketList(){
        return movieFeignClient.list();
    }
    /**
     * 需求2：购买一张电影票
     */
    @RequestMapping(value = "/buy/{userId}/{movieId}",method = RequestMethod.GET)
    public JsonResult buy(@PathVariable("userId")Long userId, @PathVariable("movieId")Long movieId){
        return userService.buy(userId,movieId);
    }
    /**
     * 需求3：新增用户时，保存用户信息，并调用电影服务，随机送一张电影票
     */
    @RequestMapping(value = "/give",method = RequestMethod.POST)
    public JsonResult give(@RequestBody User user){
        return userService.give(user);
    }
    /**
     * 需求4：传入要观看的电影Id，调用电影服务进行判断，返回提示信息
     */
    @RequestMapping(value = "/watch/{userId}/{movieId}",method = RequestMethod.GET)
    public JsonResult watch(@PathVariable("userId")Long userId, @PathVariable("movieId")Long movieId){
        return movieFeignClient.watch(userId,movieId);
    }



    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(userService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(userService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody UserQuery query){
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page);
        return JsonResult.success(new PageList<User>(page.getTotal(),page.getRecords()));
    }
}
